//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcSettingsView.h"


@implementation smcSettingsView

#pragma mark Some helpers

- (void) _setupCells
{
	struct CGRect contentRect = [UIHardware fullScreenApplicationContentRect];

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

	_usernameCell = [[UIPreferencesTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentRect.size.width, 44.0f)];
	[_usernameCell setValue: [defaults stringForKey: @"username"]];
	[_usernameCell setTitle: @"Username"];
	
	_keyCell = [[UIPreferencesTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 44.0f, contentRect.size.width, 44.0f)];
	[_keyCell setValue: [defaults stringForKey:@"personalkey"]];
	[_keyCell setTitle: @"API key"];
//	[(UITextField *) [_keyCell textField] setSecure: YES];
	
	_publicStreamCell = [[UIPreferencesTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentRect.size.width, 44.0f)];
	[_publicStreamCell setTitle: @"Public one"];
	[[_publicStreamCell textField] setEnabled: NO];
	
	_privateStreamCell = [[UIPreferencesTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 44.0f, contentRect.size.width, 44.0f)];
	[_privateStreamCell setTitle: @"Your contacts"];
	[[_privateStreamCell textField] setEnabled: NO];

	if ([[defaults stringForKey: @"stream"] isEqualToString: @"private"])
	{
		[_publicStreamCell setChecked: NO];
		[_privateStreamCell setChecked: YES];
	} else
	{
		[_publicStreamCell setChecked: YES];
		[_privateStreamCell setChecked: NO];
	}
	
	_autoupdateCell = [[UIPreferencesControlTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentRect.size.width, 44.0f)];
	[_autoupdateCell setTitle:@"Automatic refresh"];
	UISwitchControl *aSwitch = [[[UISwitchControl alloc] initWithFrame:CGRectMake(contentRect.size.width - 112.0, 9.0f, 112.0f, 44.0f)] autorelease];
	[aSwitch setValue: [defaults boolForKey:@"autoupdate"]];
	[_autoupdateCell setControl: aSwitch];

	_playSoundCell = [[UIPreferencesControlTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentRect.size.width, 44.0f)];
	[_playSoundCell setTitle:@"Play sound"];
	UISwitchControl *bSwitch = [[[UISwitchControl alloc] initWithFrame:CGRectMake(contentRect.size.width - 112.0, 9.0f, 112.0f, 44.0f)] autorelease];
	[bSwitch setValue: [defaults boolForKey:@"playsound"]];
	[_playSoundCell setControl: bSwitch];
	
	_aboutCell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0.0f, 0.0f, contentRect.size.width, 70.0f)];
	[_aboutCell setIcon: [UIImage applicationImageNamed:@"icon.png"]];
	[_aboutCell setValue: @"juhu-mac.blogspot.com"];
	[_aboutCell setDisclosureStyle: 2];
	[_aboutCell setShowDisclosure: YES];
}

#pragma mark The usual

- (void) dealloc 
{
	[_publicStreamCell release];
	[_privateStreamCell release];
	[_aboutCell release];
	[_usernameCell release];
	[_keyCell release];
	[_playSoundCell release];
	[_autoupdateCell release];
	[prefTable release];
	[topBar release];
	[super dealloc];
}

- initWithFrame: (CGRect) rect
{
	self = [super initWithFrame: rect];
	if (self != nil)
	{
		[self setBackgroundColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 1.0f]];
		
		// add navigation bar
		topBar = [[UINavigationBar alloc] initWithFrame: 
			CGRectMake(0.0f, 0.0f, rect.size.width, 44.0f)];
		
		[topBar setBarStyle: 2];
		[topBar setDelegate: self];
		[topBar showLeftButton: @"About" withStyle: 0 rightButton: @"Done" withStyle: 0];
	
		// a label
		UITextLabel *label = [[[UITextLabel alloc] initWithFrame: 
			CGRectMake(0.0f, 0.0f, rect.size.width, 44.0f)] autorelease];
		[label setText: @"Settings"];
		[label setBackgroundColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 0.0f]];
		[label setColor: [UIView colorWithRed: 1.0f green: 1.0f	blue: 1.0f alpha: 1.0f]];
		[label setCentersHorizontally: YES];

		// create cells for preferences
		[self _setupCells];
	
		// create the preferences table
		prefTable = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 44.0f, rect.size.width, rect.size.height - 44.0f)];
		[prefTable setDataSource: self];
		[prefTable setDelegate: self];
		[prefTable reloadData];

		// add as subviews
		[self addSubview: prefTable];
		[self addSubview: topBar];		
		[self addSubview: label];
	}
	
	return self;
}

#pragma mark Delegates

- (void)navigationBar: (UINavigationBar*) bar buttonClicked: (int) button 
{
	if (button == 0)
	{
		// save credentials in defaults
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		
		[defaults setObject: [_usernameCell value] forKey: @"username"];
		[defaults setObject: [_keyCell value] forKey: @"personalkey"];
		
		// stream selector
		if ([_privateStreamCell isChecked])
			[defaults setObject: @"private" forKey: @"stream"];
		else
			[defaults setObject: @"public" forKey: @"stream"];
		
		// the other options
		[defaults setBool: [[[_autoupdateCell control] valueForKey: @"value"] boolValue] forKey: @"autoupdate"];
		[defaults setBool: [[[_playSoundCell control] valueForKey: @"value"] boolValue] forKey: @"playsound"];
		
		// animate to main
		// let the controller to that...
		if ([[self delegate] respondsToSelector: @selector(animateFromTo:)])
			[[self delegate] performSelector: @selector(animateFromTo:) withObject: 
				[NSArray arrayWithObjects: self, [NSNumber numberWithInt: 0], nil]];
	} else
	{
		// show about
		UIAlertSheet *alertSheet = [[[UIAlertSheet alloc] initWithTitle: @"mobile juhu 0.4.3"
			buttons: nil
			defaultButtonIndex: 0 
			delegate: nil
			context: nil] autorelease];
		[alertSheet setBodyText: @"(C) 2007 Christian Martick"];
		[alertSheet popupAlertAnimated: YES];
	}
}

#pragma mark UIPreferencesTable delegate and data source

- (int)numberOfGroupsInPreferencesTable:(id)preferencesTable
{
	return 4;
}

- (int)preferencesTable:(id)preferencesTable numberOfRowsInGroup:(int)group
{
	switch (group)
	{
		case 0: // credentials
			return 2;
			break;
		case 1: // stream
			return 2;
			break;
		case 2: // options
			return 2;
			break;
		case 3: // about
			return 1;
			break;
	}
	return 0;
}

- (id)preferencesTable:(id)preferencesTable cellForRow:(int)row inGroup:(int)group
{
	switch (group)
	{
		case 0: // credentials
			switch (row)
			{
				case 0:
					return _usernameCell;
					break;
				case 1:
					return _keyCell;
					break;
			}
			break;
		case 1: // stream
			switch (row)
			{
				case 0:
					return _publicStreamCell;
					break;
				case 1:
					return _privateStreamCell;
					break;
			}
			break;
		case 2: // options
			switch (row)
			{
				case 0: // autoupdate
					return _autoupdateCell;
					break;
				case 1: // sound
					return _playSoundCell;
					break;
			}
			break;
		case 3: // about
			return _aboutCell;
			break;
	}
	return nil;
}

- (id)preferencesTable:(id)preferencesTable titleForGroup:(int)group
{
	switch (group)
	{
		case 0:
			return @"your jaiku account";
			break;
		case 1:
			return @"stream to follow";
			break;
		case 2:
			return @"some options";
			break;
		case 3:
			return @"more juhu";
			break;
	}
	return nil;
}

- (float)preferencesTable:(id)preferencesTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposedHeight;
{
	switch (group)
	{
		case 0: // credentials
			return 44.0f;
			break;
		case 1: // stream
			return 44.0f;
			break;
		case 2: // options
			return 44.0f;
			break;
		case 3: // about
			return 70.0f;
			break;
	}
	return 0.0;
}

- (void)tableRowSelected:(NSNotification*)notification
{
	UITableCell *selectedCell = [prefTable cellAtRow: [prefTable selectedRow] column: 0];
	
	if (selectedCell == _aboutCell)
		[(UIApplication *) [self delegate] openURL: [NSURL URLWithString: @"http://juhu-mac.blogspot.com"]];
	else if (selectedCell == _publicStreamCell)
	{
		[_publicStreamCell setChecked: YES];
		[_privateStreamCell setChecked: NO];
	} else if (selectedCell == _privateStreamCell)
	{
		[_publicStreamCell setChecked: NO];
		[_privateStreamCell setChecked: YES];
	}
}


@end
